package com.cyx.service;

import com.cyx.mapper.BookMapper;
import com.cyx.mapper.UserMapper;
import com.cyx.pojo.Book;
import com.cyx.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("userService")
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private BookMapper bookMapper;

    @Autowired
    private BookService bookService;

    public int addUser(User user){
        return userMapper.addUser(user);
    }
    public User getUser(int userId){
        return userMapper.getUserById(userId);
    }

    @Override
    public List<User> getUsers(@Nullable String userName, @Nullable String address) {
        return userMapper.getUserList(userName,address);
    }

    @Override
    public int updateUser(User user) {
        return userMapper.updateUser(user);
    }

    @Override
    @Transactional
    public int addUsers(List<User> userList) {
        int ret = 0;
        for (User user : userList) {
            ret += this.addUser(user);
        }
        return ret;
    }

    @Override
    @Transactional
    public boolean addUserAndBook(User user, Book book) throws Exception {
        //bookMapper.addBook(book);
        bookService.addBook(book);
        System.out.println("已执行Book写入");
        addUser(user);
        throw new RuntimeException("认为中断");
        //return true;
    }
}
